package com.yanqu.road.server.gameplayer.module.player;

import com.yanqu.road.entity.enums.*;
import com.yanqu.road.entity.platform.PlatformDeductIngotsOrder;
import com.yanqu.road.logic.bussiness.platform.DeductIngotBussiness;
import com.yanqu.road.logic.config.GameConfig;
import com.yanqu.road.server.agent.inter.AgentPlayer;
import com.yanqu.road.server.gameplayer.GamePlayer;
import com.yanqu.road.server.gameplayer.module.base.GeneralModule;
import com.yanqu.road.server.protocol.GameErrorCode;
import com.yanqu.road.utils.property.Property;

import java.math.BigInteger;
import java.util.*;

/**
 * 扣除元宝模块
 */
public class DeductIngotsModule extends GeneralModule {

    private List<PlatformDeductIngotsOrder> orderList;

    public DeductIngotsModule(GamePlayer player) {
        super(player);
    }

    @Override
    public boolean saveData() {
        return true;
    }

    @Override
    public boolean afterLoadData() {
        return true;
    }

    @Override
    public boolean loadData() {
        orderList = DeductIngotBussiness.getPlatformDeductIngotsOrderList(player.getUserId());
        return true;
    }

    @Override
    public void afterLogin() {

    }

    public void loginLogic() {
        if (null != orderList && orderList.size() > 0) {
            for (PlatformDeductIngotsOrder data : orderList) {
                if (data.getStatus() == eDeductIngotsStatus.Default.getValue()) {
                    doDeductIngots(data, false);
                }
            }
        }
    }

    /**
     * 充值到账，付款成功
     */
    public synchronized int doDeductIngots(PlatformDeductIngotsOrder deductIngotsOrder, boolean isAddToList) {
        //判断是否已经执行过了
        for (PlatformDeductIngotsOrder order : orderList) {
            if (order.getOrderNo().equals(deductIngotsOrder.getOrderNo())) {
                if(order.getStatus() == eDeductIngotsStatus.AlreadyDeduct.getValue()) {
                    log.error("订单已经执行扣除{}", deductIngotsOrder.getOrderNo());
                    return GameErrorCode.E_RECHARGE_FAIL;
                }
            }
        }
        log.info("订单正在扣除{}", deductIngotsOrder.getOrderNo());
        deductIngotsOrder.setStatus(eDeductIngotsStatus.AlreadyDeduct.getValue());
        Property deductIngots = new Property(GameConfig.GAME_MONEY_INGOTS, BigInteger.valueOf(deductIngotsOrder.getIngotCount()));
        if(!player.getModule(CurrencyModule.class).currencyIsEnough(deductIngots)){//不够扣除，直接封禁
            deductIngotsOrder.setExecuteType(1);
        }else {
            deductIngotsOrder.setExecuteType(0);
        }
        if (DeductIngotBussiness.updatePlatformDeductIngotsOrderStatus(deductIngotsOrder)) {
            if(!player.getModule(CurrencyModule.class).removeCurrency(deductIngots, eLogMoneyType.GM, eLogMoneyType.GmDeductIngots,
                    deductIngotsOrder.getOrderNo() + "，扣除元宝数据：" + deductIngotsOrder.getIngotCount())){//不够扣除，直接封禁
                player.getUserInfo().setBlackListEndTime(4);
                //下线处理
                AgentPlayer.executeKillPlayer(player.getUserId());
            }
            //添加到充值列表中
            if (isAddToList) {
                orderList.add(deductIngotsOrder);
            }
            return 0;
        }
        return GameErrorCode.E_RECHARGE_FAIL;
    }
}
